#!/bin/bash

DIR="$HOME/.config/dunst/icons"

function get_volume() {
    amixer -D pulse get Master | grep '%' | head -n 1 | awk -F'[' '{print $2}' | awk -F'%' '{print $1}'
}

function is_mute() {
    amixer -D pulse get Master | grep '%' | grep -oE '[^ ]+$' | grep off
}

function send_notification() {
    volume=$(get_volume)
    if is_mute; then
        icon_name="$DIR/notification-audio-volume-muted.svg"
        dunstify "Muted  " -i $icon_name -t 1000 -r 5555 -u low -h string:synchronous:audio-volume
    # fi
    else
        if [ "$volume" = "0" ]; then
        icon_name="$DIR/notification-audio-volume-muted.svg"
        elif [ "$volume" -lt "30" ]; then
            icon_name="$DIR/notification-audio-volume-low.svg"
        elif [ "$volume" -lt "70" ]; then
            icon_name="$DIR/notification-audio-volume-medium.svg"
        else
            icon_name="$DIR/notification-audio-volume-high.svg"
        fi
        dunstify "$volume%  " -i $icon_name -t 1000 -r 5555 -u low -h int:value:$(($volume))
    fi
}

case $1 in
    up)
        # Unmute
        amixer -D pulse set Master on >/dev/null
        # +2%
        amixer -D pulse set Master 2%+ >/dev/null
        ;;
    down)
        # Unmute
        amixer -D pulse set Master on >/dev/null
        # -2%
        amixer -D pulse set Master 2%- >/dev/null
        ;;
    mute)
        # Toggle mute
        amixer -D pulse set Master 1+ toggle >/dev/null
        ;;
esac
send_notification
